﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSBTC.Model;

namespace MSBTC.WinForms
{
    public partial class EquipmentListForm : Form
    {
        private string Datamanip;
        private int rowselect;
        MDIParent mdipar = new MDIParent();
        PMSContext dbcontext = new PMSContext();
        Equipment dbequip = new Equipment();
        AssignEquipForm PrjEq = new AssignEquipForm();

        public EquipmentListForm()
        {
            InitializeComponent();
        }

        private void LLAssgn_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            PrjEq.Show();
        }

        private void EquipmentListForm_Load(object sender, EventArgs e)
        {
            RefreshDB();
        }

     
        #region functions
        private void RefreshDB()
        {
            var equ = (from e in dbcontext.Equipments
                       select e).ToList();

            LVEquipListInfo.Items.Clear();
            if (equ != null)
            {

                foreach (var sqlEqu in equ)
                {
                    ListViewItem lvitems = LVEquipListInfo.Items.Add(sqlEqu.Equipmentid_PK.ToString());
                    lvitems.SubItems.Add(sqlEqu.Equip_Name.ToString());
                    lvitems.SubItems.Add(sqlEqu.Brand.ToString());
                    lvitems.SubItems.Add(sqlEqu.Quantity.ToString());
                    lvitems.SubItems.Add(sqlEqu.Description.ToString());
                   

                }
            }
            this.Refresh();
        }



        private void DisableFields()
        {
            //txtCname.Enabled = false;
            //txtAddr.Enabled = false;
            //txtCity.Enabled = false;
            //txtDesc.Enabled = false;
            //txtEadd.Enabled = false;
            //txtCnum.Enabled = false;

        }

        private void EnableFields()
        {
            //txtCname.Enabled = true;
            //txtAddr.Enabled = true;
            //txtCity.Enabled = true;
            //txtDesc.Enabled = true;
            //txtEadd.Enabled = true;
            //txtCnum.Enabled = true;

        }

        private void RefreshFields()
        {
            DisableFields();
            RefreshDB();

            //btnCNew.Enabled = true;
            //btnCDel.Enabled = false;
            //btnCEdit.Enabled = false;
            //btnClear.Enabled = true;
            //btnCSave.Enabled = false;
        }
        #endregion


        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            var equ = (from eq in dbcontext.Equipments
                       where eq.Equip_Name.Contains(txtSearch.Text)
                       select eq).ToList();

            LVEquipListInfo.Items.Clear();
            if (equ != null)
            {

                foreach (var sqlEqu in equ)
                {
                    ListViewItem lvitems = LVEquipListInfo.Items.Add(sqlEqu.Equipmentid_PK.ToString());
                    lvitems.SubItems.Add(sqlEqu.Equip_Name.ToString());
                    lvitems.SubItems.Add(sqlEqu.Brand.ToString());
                    lvitems.SubItems.Add(sqlEqu.Quantity.ToString());
                    lvitems.SubItems.Add(sqlEqu.Description.ToString());
                    

                }
                
                if (txtSearch.Text == "")
                {
                    RefreshDB();
                }
            }
        }

      
    }
}
